<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>基础算数题练习系统</title>
    <style>
        :root {
            --primary-color: #4CAF50;
            --error-color: #f44336;
            --bg-color: #f5f5f5;
        }

        body {
            font-family: 'Segoe UI', Tahoma, sans-serif;
            background-color: var(--bg-color);
            margin: 0;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 3px 15px rgba(0,0,0,0.1);
        }

        .settings-panel {
            padding: 20px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            margin-bottom: 25px;
        }

        .control-group {
            margin: 15px 0;
        }

        .operations {
            display: flex;
            gap: 15px;
            margin: 15px 0;
        }

        .operation-label {
            display: flex;
            align-items: center;
            padding: 8px 15px;
            background: #f8f9fa;
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.2s;
        }

        .operation-label:hover {
            background: #e9ecef;
        }

        input[type="checkbox"] {
            margin-right: 8px;
        }

        .num-input {
            padding: 8px 12px;
            border: 2px solid #ddd;
            border-radius: 6px;
            width: 120px;
            font-size: 16px;
        }

        .btn {
            padding: 10px 25px;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.2s;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 3px 8px rgba(0,0,0,0.15);
        }

        #questions {
            margin: 25px 0;
        }

        .question-item {
            padding: 15px;
            margin: 10px 0;
            border-left: 4px solid transparent;
            border-radius: 6px;
            background: #fff;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .question-item.correct {
            border-color: var(--primary-color);
            background: #e8f5e9;
        }

        .question-item.incorrect {
            border-color: var(--error-color);
            background: #ffebee;
        }

        .answer-input {
            padding: 8px 12px;
            border: 2px solid #ddd;
            border-radius: 6px;
            width: 100px;
            font-size: 16px;
        }

        #result {
            margin-top: 25px;
            padding: 15px;
            border-radius: 6px;
            font-size: 18px;
            font-weight: bold;
            text-align: center;
        }

        .correct-answer {
            color: var(--error-color);
            margin-left: 15px;
            font-size: 14px;
        }
    </style>
    <style>
        .info-icon {
            position: absolute;
            right: 30px;
            top: 25px;
            width: 20px;
            height: 20px;
            line-height: 20px;
            text-align: center;
            border-radius: 50%;
            background-color: #ccc;
            color: white;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.2s;
        }

        .info-icon:hover {
            background-color: #999;
        }

        .info-tooltip {
            position: absolute;
            right: 60px;
            top: 50px;
            background-color: white;
            padding: 15px;
            border-radius: 6px;
            box-shadow: 0 3px 15px rgba(0,0,0,0.1);
            z-index: 1000;
            display: none;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="settings-panel">
        <h2 onclick="toggleSettings()" style="cursor: pointer;">题目设置 ▼
            <span class="info-icon" onclick="showInfo(event)">?</span>
        </h2>
        <div id="settingsContent">
            <div class="control-group">
                <label>运算类型：</label>
                <div class="operations">
                    <label class="operation-label">
                        <input type="checkbox" name="op" value="+" checked> 加法
                    </label>
                    <label class="operation-label">
                        <input type="checkbox" name="op" value="-"> 减法
                    </label>
                    <label class="operation-label">
                        <input type="checkbox" name="op" value="*"> 乘法
                    </label>
                    <label class="operation-label">
                        <input type="checkbox" name="op" value="/"> 除法
                    </label>
                </div>
            </div>
            <div class="control-group">
                <label>参与算数的范围：</label>
                <select id="operandRange" class="num-input">
                    <option value="5">5以内</option>
                    <option value="10">10以内</option>
                    <option value="30">30以内</option>
                    <option value="50">50以内</option>
                    <option value="100">100以内</option>
                    <option value="1000">1000以内</option>
                </select>
            </div>
            <div class="control-group">
                <label>答案范围：</label>
                <select id="answerRange" class="num-input">
                    <option value="10">10以内</option>
                    <option value="20">20以内</option>
                    <option value="30">30以内</option>
                    <option value="50">50以内</option>
                    <option value="100">100以内</option>
                    <option value="200">200以内</option>
                    <option value="500">500以内</option>
                    <option value="1000">1000以内</option>
                    <option value="10000">10000以内</option>
                </select>
            </div>
            <div class="control-group">
                <label>题目数量：</label>
                <input type="number" id="questionCount" class="num-input" min="1" max="100" value="20">
                <button class="btn" onclick="generateQuestions()">生成题目</button>
            </div>
        </div>
    </div>

    <div id="questions"></div>

    <div style="text-align: center;">
        <button class="btn" onclick="submitTest()" style="background: #2196F3;">交卷批改</button>
    </div>

    <div id="result"></div>
</div>

<script>
    function showInfo(event) {
        event.stopPropagation();
        const tooltip = document.createElement('div');
        tooltip.className = 'info-tooltip';
        tooltip.innerHTML = `
            <p><b>基础算数规则：</b></p>
            <ul>
                <li>加法：a + b = c</li>
                <li>减法：a - b = c (a ≥ b)</li>
                <li>乘法：a × b = c</li>
                <li>除法：a ÷ b = c (b ≠ 0即除数不能为0)</li>
            </ul>
        `;
        document.body.appendChild(tooltip);
        tooltip.style.display = 'block';

        const hideTooltip = () => {
            tooltip.style.display = 'none';
            document.body.removeChild(tooltip);
        };

        setTimeout(hideTooltip, 15000);
        document.addEventListener('click', hideTooltip, { once: true });
    }

    function toggleSettings() {
        const settingsContent = document.getElementById('settingsContent');
        const header = document.querySelector('.settings-panel h2');
        if (settingsContent.style.display === 'none') {
            settingsContent.style.display = 'block';
            header.innerHTML = '题目设置 ▼';
        } else {
            settingsContent.style.display = 'none';
            header.innerHTML = '题目设置 ▶';
        }
    }

    function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function generateQuestions() {
        const count = Math.min(parseInt(document.getElementById('questionCount').value), 100);
        const ops = Array.from(document.querySelectorAll('input[name="op"]:checked')).map(o => o.value);
        const operandRange = parseInt(document.getElementById('operandRange').value);
        const answerRange = parseInt(document.getElementById('answerRange').value);
        currentQuestions = [];
        let existingQuestions = new Set(); // 用于存储已生成的题目

        let questionsHTML = '';
        for (let i = 0; i < count; i++) {
            const op = ops[Math.floor(Math.random() * ops.length)];
            let a, b, answer;
            let valid = false;
            let questionKey;

            while (!valid) {
                switch(op) {
                    case '+':
                        a = getRandomInt(1, operandRange);
                        b = getRandomInt(1, operandRange);
                        answer = a + b;
                        break;
                    case '-':
                        a = getRandomInt(1, operandRange);
                        b = getRandomInt(1, a);
                        answer = a - b;
                        break;
                    case '*':
                        a = getRandomInt(1, Math.min(20, operandRange));
                        b = getRandomInt(1, Math.min(10, operandRange));
                        answer = a * b;
                        break;
                    case '/':
                        answer = getRandomInt(1, Math.min(10, operandRange));
                        b = getRandomInt(1, Math.min(10, operandRange));
                        a = b * answer;
                        break;
                }

                questionKey = `${a}${op}${b}`; // 生成题目唯一标识
                if (answer <= answerRange && !existingQuestions.has(questionKey)) {
                    valid = true;
                    existingQuestions.add(questionKey); // 将题目添加到已生成集合中
                }
            }

            currentQuestions.push({
                id: i,
                a,
                b,
                op,
                answer,
                userAnswer: null
            });

            questionsHTML += `
                    <div class="question-item" id="q${i}">
                        <span>第 ${i+1} 题：</span>
                        <span class="problem">${a} ${op} ${b} =</span>
                        <input type="number" class="answer-input" id="ans${i}">
                    </div>
                `;
        }

        document.getElementById('questions').innerHTML = questionsHTML;
        document.getElementById('result').innerHTML = '';
    }

    function submitTest() {
        let correctCount = 0;
        currentQuestions.forEach(q => {
            const userAnswer = parseInt(document.getElementById(`ans${q.id}`).value);
            q.userAnswer = userAnswer;
            const questionElement = document.getElementById(`q${q.id}`);
            if (userAnswer === q.answer) {
                correctCount++;
                questionElement.classList.add('correct');
                questionElement.classList.remove('incorrect');
            } else {
                questionElement.classList.add('incorrect');
                questionElement.classList.remove('correct');
                const correctAnswer = document.createElement('span');
                correctAnswer.className = 'correct-answer';
                correctAnswer.textContent = `正确答案: ${q.answer}`;
                questionElement.appendChild(correctAnswer);
            }
        });

        const result = document.getElementById('result');
        result.textContent = `得分: ${correctCount}/${currentQuestions.length}`;
        result.style.backgroundColor = correctCount === currentQuestions.length ? '#e8f5e9' : '#ffebee';
        result.style.color = correctCount === currentQuestions.length ? '#4CAF50' : '#f44336';
    }

    // 初始生成题目
    generateQuestions();
</script>
</body>
</html>